<?php
namespace haibao\app\data;

class AppUpdate extends BaseMysql{
    public function __construct(){
        parent::__construct('\haibao\app\model\data\AppUpdate');
    }
    
    public function getAppUpdateList($filter){
        return $this->getAll($filter);
    }
    
    public function changeStatus($id, $status,$appType,$version){
        $sql = 'update AppUpdate set Status = %s where Id = %s';
        $this->query($sql,array($status,$id));
        
        $cache = new \haibao\app\cache\Update();
        $cache->deleteVersion($appType.$version);
        if($status == \haibao\app\model\data\AppUpdate::STATUS_ENABLE){
            $this->setVersionCache($id);
        }
    }
    
    public function getUpdateById($id){
        return  $this->getOneById($id);
    }
    
    public function getOneUpdate($version,$appType){
        $sql = 'select Id from AppUpdate where Version = %s and AppType = %s and Status <> %s';
        return $this->query($sql,array($version,$appType,\haibao\app\model\data\AppUpdate::STATUS_DELETE))->fetch_assoc();
    }
    
    public function saveUpdate($model){
        $this->beginTranscation();
        try {
            if($model->Id){
                $this->save($model);
                $cache = new \haibao\app\cache\Update();
                $cache->deleteVersion($model->AppType.$model->Version);
                $this->setVersionCache($model->Id);
            }else {
                $this->add($model);
            }
        } catch (\Exception $e) {
            $this->rollback();
            throw $e;
        }
        $this->endTranscation();
    }
    
    public function getUpdateByAppType($appType,$appVersion){
        $cache = new \haibao\app\cache\Update();
        return $cache->getVersion($appType.$appVersion);
    }
    
    private function setVersionCache($id){
        $vesionList = $fileName = array();
        $sql = 'select AppType,Version,Url,ImageId from AppUpdate where Id=%s and Status=%s';
        $result = $this->query($sql,array($id,\haibao\app\model\data\AppUpdate::STATUS_ENABLE));
        while($result->fetch_assoc()){
            $vesionList['Url'] = $result->getData('Url');
            $vesionList['Version'] = $result->getData('Version');
            $vesionList['ImageId'] = $result->getData('ImageId');
            $vesionList['AppType'] = $result->getData('AppType');
            break;
        }
        if(isset($vesionList['ImageId']) && $vesionList['ImageId']){
            $fileName = \haibao\cms\business\ImageLibrary::getImageUrlById($vesionList['ImageId']);
            if(!empty($fileName)){
                $vesionCacheList = array();
                $vesionCacheList['fileName'] = $fileName[$vesionList['ImageId']];
                $vesionCacheList['Url'] = $vesionList['Url'];
                $cache = new \haibao\app\cache\Update();
                $cache->setVersion($vesionList['AppType'].$vesionList['Version'], $vesionCacheList);
            }
        }
    }
    
    private function addImage($model){
        $this->add($model);
    }
    
    
}